<!--
 * @Author: 许浩锋
 * @Date: 2021-06-22 14:25:54
 * @LastEditors: 许浩锋
 * @LastEditTime: 2021-07-03 18:33:48
 * @Description:
-->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            clearable
            @clear="getGoodsList"
            placeholder="请输入内容"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="goAddpage" type="primary"> 添加商品 </el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border :stripe="true">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="75">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70">
        </el-table-column>
        <el-table-column prop="add_time" label="商创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.goods_id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: { query: '', pagenum: 1, pagesize: 10 },
      addioLogVisible: false,
      addForm: {
        goods_name: '',
        goods_cat: '',
        goods_number: '',
        goods_weight: '',
      },
      addFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_cat: [
          { required: true, message: '以为,分割的分类列表', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入数量', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入重量', trigger: 'blur' },
        ],
      },
      total: 0,
    }
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get(`goods`, {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限列表失败')
      }
      this.$message.success('获取权限列表成功')
      this.total = res.data.total
      this.goodsList = res.data.goods
      console.log(this.goodsList)
    },
    async addGoods() {
      const { data: res } = await this.$http.post(`goods`, {})
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    removeById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          console.log(id)
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if (res.meta.status !== 200) {
            return this.$message.error('删除失败')
          }
          console.log(res)
          this.$message.success('删除成功')
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    goAddpage() {
      this.$router.push('/add')
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>
<style lang="less" scoped>
</style>
